/**
 * Project hdmap
 * @author shengjialiang
 * @version v1.0
 */


#ifndef _POINTPICKERTOOL_H
#define _POINTPICKERTOOL_H

#include <osgGA/GUIEventHandler>
#include "map_tool/ToolConfig.h"
#include "map_common/IEvent.h"
#include "map_common/IEventHandler.h"
#include "map_datarender/GeneralRenderObject.h"

namespace Tool
{
	struct SelectObject
	{
		SelectObject()
		{
			RenderObject = NULL;
			PrimitiveIndex = -1;
			LastPoint = osg::Vec3d(0, 0, 0);
			IntersectPoint = osg::Vec3d(0, 0, 0);
		}

		inline bool isNull()
		{
			return (RenderObject == NULL || PrimitiveIndex < 0);
		}

		inline void setNull()
		{
			RenderObject = NULL;
			PrimitiveIndex = -1;
		}

		DataRender::IRenderObject* RenderObject;
		osg::Vec3d IntersectPoint;
		osg::Vec3d LastPoint;
		int PrimitiveIndex;
	};

	class MAP_TOOL_EXPORT  IPointsPickerTool
        : public osgGA::GUIEventHandler,public Common::CEventHandlerRegister
    {
    public:

        IPointsPickerTool(osg::ref_ptr<osg::Group> in_pRefRootNode);

        virtual ~IPointsPickerTool();

		virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa);

		virtual void reset(const bool& clearResult = false);

		static osg::Vec3d getIntersectedPoint(
			float x, float y, float alt, osgGA::GUIActionAdapter& us);
    
		std::vector<osg::Vec3d>& getPoints()
		{
			return this->mPoints;
		}
    protected:
		SelectObject getIntersectObject(
			const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa,
			DataRender::CPointRenderObject* pPointEditRenderObject = NULL, const std::string& renderType = "");

        virtual void initial();

		virtual bool notifyAddPoint(const osg::Vec3d& in_point);

		virtual bool notifyDragPoint(const osg::Vec3d& in_point,const int& in_index);

		virtual bool notifyRemovePoint(const osg::Vec3d& in_point);

		virtual void keyPressEvent(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
		{

		}
		virtual void keyReleaseEvent(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
		{

		}
		virtual void mousePressEvent(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aat)
		{

		}
		virtual void mouseDoubleClickEvent(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
		{

		}
		virtual void mouseReleaseEvent(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
		{

		}

		virtual void mouseMoveEvent(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
		{

		}
		virtual void wheelEvent(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
		{

		}

	private:
		void updatePointEditRender(const int& in_pointIndex,
			const osg::Vec4& in_pointColor = osg::Vec4(-1.0,0.0,0.0,1.0),
			const osg::Vec3d& in_pointPos = osg::Vec3d(-100000000, -100000000, -100000000));

		void generateCoffiLine();
	public:
		std::vector<osg::Vec3d> CloudPoints;
    protected:
        float mX, mY;
        float mXpick, mYpick;
        bool mHasMouseDown;
        bool mHasMouseDrag;
        std::vector<osg::Vec3d> mPoints;
        osg::ref_ptr<osg::Group> mpRefRootNode;
		SelectObject mSelectObject;
		osg::Vec4 mPointColor;
		osg::Vec4 mHighColor;
		DataRender::CPointRenderObject* mpPointEditRenderObject;
    };

}

#endif //_POINTPICKERTOOL_H